package com.bookstore.dao.imp;

import com.bookstore.bean.ShoppingCartItem;
import com.bookstore.dao.BookDao;
import com.bookstore.bean.Book;
import com.bookstore.web.Page;
import com.bookstore.web.CriteriaBook;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Admin on 2016/8/11.
 */
public class BookDaoImp extends BaseDao<Book> implements BookDao {
    @Override
    public Book getBook(int id) {
        String sql = "SELECT id, author, title, price, publishingDate, " +
                "salesAmount, storeNumber, remark FROM mybooks WHERE id = ?";
        return get(sql, id);
    }

    @Override
    public long getCountWithTitle(String title) {
        String sql="select count(id) from mybooks where title=?";
        return getForValue(sql,title);
    }


    /**
     * 根据传入的CriteriaBook 对象返回对应的 Page 对象
     *相当于模糊查询,获取页面
     * @param cb
     * @return
     */
    @Override
    public Page<Book> getPage(CriteriaBook cb) {
        Page<Book> page=new Page(cb.getPageNo());
        //根据传入的cb 获取符合条件的总记录数
        //然后设置为符合该记录数
        page.setTotalItemNumber(getTotalBookNumber(cb));
        //校验 pageNo 的合法性
        cb.setPageNo(page.getPageNo());
        //根据传入的CriteriaBook 和 pageSize 返回当前页对应的List
        page.setList(getPageList(cb,3));
        return page;
    }

    /**
     * 根据传入的CriteriaBook 对象返回对应的 记录数
     * @param cb
     * @return
     */
    @Override
    public long getTotalBookNumber(CriteriaBook cb) {
        String sql="select count(id) from mybooks where price>=? and price<=? ";
        return getForValue(sql,cb.getMinPrice(),cb.getMaxPrice());
    }

    @Override
    public List<Book> getPageList(CriteriaBook cb, int pageSize) {
        String sql = "SELECT id, author, title, price, publishingDate, " +
                "salesAmount, storeNumber, remark FROM mybooks " +
                "WHERE price >= ? AND price <= ? " +
                "LIMIT ?, ?";
        return getForList(sql,cb.getMinPrice(),cb.getMaxPrice(),(cb.getPageNo() -1)* pageSize,pageSize);
    }

    @Override
    public int getStoreNumber(Integer id) {
        String sql = "SELECT storeNumber FROM mybooks WHERE id = ?";
        return getForValue(sql, id);
    }

    @Override
    public void batchUpdateStoreNumberAndSalesAmount(Collection<ShoppingCartItem> items) {
        String sql="update mybooks set salesAmount=salesAmount+?,storeNumber=storeNumber-? " +
                "where id=?";
        Object [][] params = null;
        params = new Object[items.size()][3];
        List<ShoppingCartItem> sci=new ArrayList<>(items);
        for(int i=0;i< items.size();i++){
            params[i][0]=sci.get(i).getQuantity();
            params[i][1]=sci.get(i).getQuantity();
            params[i][2]=sci.get(i).getBook().getId();
        }
        batch(sql,params);

    }

    @Override
    public void save(Book book) {
        String sql = "INSERT INTO mybooks" +
                "(author,title,price,publishingDate,salesAmount,storeNumber,remark) VALUES(?,?,?,?,?,?,?)";
        update(sql,book.getAuthor(),book.getTitle(),book.getPrice(),book.getPublishingDate(),book.getSalesAmount(),
        book.getStoreNumber(),book.getRemark());
    }
}
